package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateCoopCom
//@description: 创建CoopCom记录
//@param: coopCom model.CoopCom
//@return: err error

func CreateCoopCom(coopCom model.CoopCom) (err error) {
	err = global.GVA_DB.Create(&coopCom).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCoopCom
//@description: 删除CoopCom记录
//@param: coopCom model.CoopCom
//@return: err error

func DeleteCoopCom(coopCom model.CoopCom) (err error) {
	err = global.GVA_DB.Delete(&coopCom).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCoopComByIds
//@description: 批量删除CoopCom记录
//@param: ids request.IdsReq
//@return: err error

func DeleteCoopComByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.CoopCom{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCoopCom
//@description: 更新CoopCom记录
//@param: coopCom *model.CoopCom
//@return: err error

func UpdateCoopCom(coopCom model.CoopCom) (err error) {
	err = global.GVA_DB.Save(&coopCom).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCoopCom
//@description: 根据id获取CoopCom记录
//@param: id uint
//@return: err error, coopCom model.CoopCom

func GetCoopCom(id uint) (err error, coopCom model.CoopCom) {
	err = global.GVA_DB.Where("id = ?", id).First(&coopCom).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCoopComInfoList
//@description: 分页获取CoopCom记录
//@param: info request.CoopComSearch
//@return: err error, list interface{}, total int64

func GetCoopComInfoList(info request.CoopComSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.CoopCom{})
    var coopComs []model.CoopCom
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Name != "" {
        db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
    }
    if info.Url != "" {
        db = db.Where("`url` LIKE ?","%"+ info.Url+"%")
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&coopComs).Error
	return err, coopComs, total
}